package cn.kzgg.basic.controller;


import cn.kzgg.basic.dto.RegisterDto;
import cn.kzgg.basic.exception.BusinessException;
import cn.kzgg.basic.service.IVerifyCodeService;
import cn.kzgg.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key ){
        try {
            String  base64Str=verifyCodeService.imageCode(key);
            return AjaxResult.show().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }


    }


    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
           verifyCodeService.smsCode(dto);
            return AjaxResult.show();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }


    }
}
